<template>
 <div id="box">
    <div class="login_containter"> 
    <div class="login_box">
        <!--头像区域-->
     <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
     </div>
     <!--表单区域-->
     <el-form :model="loginForm" ref="loginFormRef"  label-width="0px" class="login_form"  :rules="ruleds">
        <!--用户名-->
  <el-form-item prop="username">
    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" type="text"></el-input>
  </el-form-item>
  <!--密码-->
  <el-form-item label="" prop="password">
    <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
  </el-form-item>
  <!--按钮-->
  <el-form-item class="btns">
     <el-button type="primary" @click="login()">登入</el-button>
  <el-button type="info" @click="reset()" >重置</el-button>
  </el-form-item>
 
     </el-form>
    </div>
    </div>
  
 </div>
    </template>

<script>
export default {
    name: "login",
    data: function () {
        return {
            loginForm: {
                username: '',
                password: "",
            },
            ruleds: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '字符长度在3-10', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: "请填写密码", trigger: 'blur' },
                    { min: 5, max: 15, message: "字符长度为5-15", trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        reset() {
            this.$refs.loginFormRef.resetFields()
        },
        async login() {
            this.$refs.loginFormRef.validate(async (bool) => {
                if (!bool) return;
                const { data } = await this.$http.post("/login", this.loginForm)
                if (data.meta.status !== 200) return this.$message.error("用户名或密码有误，登入失败")
                window.sessionStorage.setItem("login", data.data.token)
                this.$message.success("登入成功")
                this.$router.push("/home")

            })
        }
    }
}
</script>

<style lang="less" scoped>
.login_containter {
    height: 100%;
    background-color: #2b4b6b;
}

.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -225px;
    margin-top: -150px;

    .avatar_box {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 100%;
        padding: 10px;
        box-shadow: 0px 0px 10px #ddd;
        position: absolute;
        left: 50%;
        margin-left: -65px;
        margin-top: -65px;
        background-color: #fff;

        img {
            width: 100%;
            height: 100%;
            border-radius: 100%;
            background-color: #eee;
        }
    }
}

.btns {
    display: flex;
    justify-content: space-between
}

.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 0px 20px;
}
</style>